package com.itqf.controller;

import com.google.gson.Gson;
import com.itqf.entity.Product;
import com.itqf.entity.Type;
import com.itqf.service.OrdersService;
import com.itqf.service.ProductService;
import com.itqf.service.TypeService;
import com.itqf.service.impl.OrdersServiceImpl;
import com.itqf.service.impl.ProductServiceImpl;
import com.itqf.service.impl.TypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;


@WebServlet("/type")
public class TypeController extends BaseServlet{
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        TypeService typeService = new TypeServiceImpl();
        List<Type> types = typeService.findAll();

        Gson gson = new Gson();
        String json = gson.toJson(types);
        return json;
    }

    public String findTypeBypid(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String pid = request.getParameter("pid");
//        System.out.println(pid);


        ProductService productService = new ProductServiceImpl();
//        获取tid
        Product product = productService.findProductByPid(pid);
        int tid = product.getTid();
//        System.out.println(tid);

//        获取typename
        TypeServiceImpl typeService = new TypeServiceImpl();
        Type type = typeService.findTypeByTid(tid);

//        System.out.println(type);
        Gson gson = new Gson();
        String json = gson.toJson(type);
        return json;
    }
}
